package com.qf.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.CarTypeMapper;
import com.qf.ssm.po.CarType;
import com.qf.ssm.qo.CarTypeQo;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CarTypeService;
import com.qf.ssm.utlis.CommonBeanutils;
import com.qf.ssm.vo.CarTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class CarTypeServiceImpl implements CarTypeService{

    @Autowired
    private CarTypeMapper carTypeMapper;

    @Override
    public Integer saveCarType(CarTypeQo carTypeQo) {
        Integer count = carTypeMapper.insertCarType(CommonBeanutils.convertTo(carTypeQo, CarType::new));
        if (count<1){
            throw new ServiceException(ResultStatus.SQL_DELETE_ERROR);
        }
        return count;
    }

    @Override
    public PageInfo<CarTypeVo> carTypeList(int page,int size,CarTypeQo carTypeQo) {
        PageInfo<CarTypeVo> carTypeVoPageInfo = CommonBeanutils.convertPageInfo(PageHelper.startPage(page, size).doSelectPageInfo(() ->
                carTypeMapper.selectCarTypeList(carTypeQo)), new PageInfo<>(), CarTypeVo::new);
        if (ObjectUtils.isEmpty(carTypeVoPageInfo.getList())){
            throw new ServiceException(ResultStatus.TYPE_NO_EXIST);
        }
        return carTypeVoPageInfo;
    }

    @Override
    public Integer modifyCarType(Integer tid, CarTypeQo carTypeQo) {
        Integer count = carTypeMapper.updateCarTypeById(tid, CommonBeanutils.convertTo(carTypeQo, CarType::new));
        if (count<1){
            throw new ServiceException(ResultStatus.SQL_UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public Integer removeCarType(Integer tid) {
        Integer count = carTypeMapper.deleteCarTypeById(tid);
        if (count<1){
            throw new ServiceException(ResultStatus.SQL_DELETE_ERROR);
        }
        return count;
    }

    @Override
    public CarTypeVo queryCarType(Integer tid) {
        CarTypeVo carTypeVo = CommonBeanutils.convertTo(carTypeMapper.selectCarTypeById(tid), CarTypeVo::new);
        if (ObjectUtils.isEmpty(carTypeVo)){
            throw new ServiceException(ResultStatus.TYPE_NO_EXIST);
        }
        return carTypeVo;
    }
}
